package com.apes.fn.fico.reimbursement.repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.query.ApesDao;
import com.apes.framework.util.DateUtil;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * Created by lishikun on 2021/1/29 16:23
 */
@Component
public class TransactionFlowDao extends ApesDao {

    /**
     * 批量查询交易流水号
     * @param param
     * @return
     */
    public JSONArray queryAllTransactionFlow(JSONObject param) {
        JSONObject request = new JSONObject();
        String sql = " SELECT T.* FROM fn_transaction_flow t where 1=1" ;
        if (Objects.nonNull(param.get("id"))) {
            request.put("id", param.getString("id"));
            sql += " and (t.id = :id or t.transaction_flow_num = :id)";
        }
        if (Objects.nonNull(param.get("transactionDate")) && param.getJSONArray("transactionDate").size() > 0) {
            Date transactionDate = StringToDate(param.getJSONArray("transactionDate").getString(0),DateUtil.SHOW_DATETIME_FORMAT);
            String transactionDates = DateUtil.format(transactionDate,DateUtil.SHOW_DATE_FORMAT);
            sql += " AND trunc(t.transaction_date) >= " + "to_date('"+ transactionDates +"', 'yyyy-mm-dd')";
            transactionDate = StringToDate(param.getJSONArray("transactionDate").getString(1),DateUtil.SHOW_DATE_FORMAT);
            transactionDates = DateUtil.format(transactionDate,DateUtil.SHOW_DATE_FORMAT);
            sql += " AND trunc(t.transaction_date) <= " + "to_date('" + transactionDates + "','yyyy-mm-dd')";
        }
        if (Objects.nonNull(param.get("createDate")) && param.getJSONArray("createDate").size() > 0) {
            Date createDate = StringToDate(param.getJSONArray("createDate").getString(0),DateUtil.SHOW_DATETIME_FORMAT);
            String createDates = DateUtil.format(createDate,DateUtil.SHOW_DATE_FORMAT);
            sql += " AND trunc(t.create_date) >= " + "to_date('"+ createDates +"', 'yyyy-mm-dd')";
            createDate = StringToDate(param.getJSONArray("createDate").getString(1),DateUtil.SHOW_DATE_FORMAT);
            createDates = DateUtil.format(createDate,DateUtil.SHOW_DATE_FORMAT);
            sql += " AND trunc(t.create_date) <= " + "to_date('" + createDates + "','yyyy-mm-dd')";
        }
        if (Objects.nonNull(param.get("accountId"))) {
            request.put("accountId", param.getString("accountId"));
            sql += " and (t.payment_account_id = :accountId or t.counterparty_account_num = :accountId)";
        }
        if (Objects.nonNull(param.get("transactionFlowNum"))) {
            request.put("transactionFlowNum", param.getString("transactionFlowNum"));
            sql += " and t.transaction_flow_num = :transactionFlowNum ";
        }
        if (Objects.nonNull(param.get("state"))) {
            request.put("state", param.getJSONObject("state").getString("id"));
            sql += " and t.state = :state ";
        }
        return toJSONArray(find(sql, request));
    }

    /**
     * 获取查选条件字符串
     * @param sql sql 语句
     * @param array 条件数组
     * @return 字符串
     */
    private String getQueryString(String sql, JSONArray array) {
        StringBuilder sqlBuilder = new StringBuilder(sql);
        for (int i = 0; i < array.size(); i++) {
            JSONObject json = array.getJSONObject(i);
            if (i == 0) {
                sqlBuilder.append("'").append(json.getString("id")).append("'");
            } else {
                sqlBuilder.append(",'").append(json.getString("id")).append("'");
            }
        }
        sqlBuilder.append(")");
        sql = sqlBuilder.toString();
        return sql;
    }

    /**
     * String --> Date
     * @param endDate
     * @param dateFormat
     * @return
     */
    public Date StringToDate(String endDate, String dateFormat){
        DateFormat format = new SimpleDateFormat(dateFormat);
        Date eDate = null;
        try {
            eDate = format.parse(endDate);
        }catch  (ParseException e) {
            e.printStackTrace();
        }
        return eDate;
    }
}
